package com.example.pceb.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.pceb.entity.IpTransaction;
import com.example.pceb.minIO.MinioService;
import com.example.pceb.result.Result;
import com.example.pceb.service.IpTransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@RestController
//@CrossOrigin(origins = "*")
@RequestMapping("/api/ip")
public class IpTransactionController {

    @Autowired
    private IpTransactionService ipTransactionService;

    // 获取分页专利市场列表  6.30
    @GetMapping("/market")
    public Result<IPage<IpTransaction>> getMarket(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "30") int size
    ) {
        return Result.success(ipTransactionService.findPage(page, size, "在售"));
    }

    // 发布专利  6.30
    @PostMapping("/publish")
    public Result<?> publish(@RequestBody IpTransaction ip) {
        ip.setStatus("在售");
        ip.setTransferConfirmed(false);
        ipTransactionService.save(ip);
        return Result.success();
    }

    // 查看自己发布的
    @GetMapping("/my")
    public Result<List<IpTransaction>> mySales(@RequestParam String username) {
        return Result.success(ipTransactionService.findBySeller(username));
    }

    //删除专利
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Long id, @RequestParam String username) {
        System.out.println("开始删除");
        IpTransaction ip = ipTransactionService.getById(id);
        if (ip == null || !ip.getSellerUsername().equals(username)) {
            return Result.error("无权限或专利不存在");
        }
        ipTransactionService.removeById(id);
        return Result.success("删除成功");
    }

    // 买家点击购买，设置 buyerUsername   6.30
    @PostMapping("/buy")
    public Result<?> buy(@RequestBody Map<String, Object> map) {
        try {
            Long id = Long.valueOf(map.get("id").toString());
            String buyer = map.get("buyerUsername").toString();

            System.out.println("开始获取 IP 交易");
            IpTransaction ip = ipTransactionService.getById(id);

            System.out.println("获取到 IP：" + ip);
            if (ip == null || !"在售".equals(ip.getStatus())) {
                return Result.error("商品不存在或已售出");
            }

            System.out.println("准备设置 buyerUsername");
            ip.setBuyerUsername(buyer);
            System.out.println("buyerUsername 设置为：" + ip.getBuyerUsername());

            ipTransactionService.updateById(ip);
            return Result.success(ip.getQrCodeUrl());
        } catch (Exception e) {
            e.printStackTrace(); // 打印异常栈
            return Result.error("后端异常：" + e.getMessage());
        }
    }
    //minIO上传二维码收款 7.1
    @Autowired
    private MinioService minioService;

    @PostMapping("/uploadQr")
    public Result<?> uploadQr(@RequestParam("file") MultipartFile file) {
        try {
            String url = minioService.upload(file);
            System.out.println(url);
            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传失败: " + e.getMessage());
        }
    }


    // 卖家确认转让  6.30
    @PostMapping("/confirm")
    public Result<?> confirmTransfer(@RequestBody Map<String, Object> map) {
        Long id = Long.valueOf(map.get("id").toString());
        IpTransaction ip = ipTransactionService.getById(id);
        System.out.println(ip.getBuyerUsername());
        if (ip == null || ip.getBuyerUsername() == null) return Result.error("未设置买家");
        ip.setSellerUsername(ip.getBuyerUsername());
        ip.setBuyerUsername(null);
        ip.setTransferConfirmed(true);
        ip.setStatus("已售");
        ipTransactionService.updateById(ip);
        return Result.success();
    }
}

